package controller

import (
	"ahutoj/web/io/constanct"
	"ahutoj/web/io/request"
	"ahutoj/web/io/response"
	"ahutoj/web/logic"
	"ahutoj/web/middlewares"
	"ahutoj/web/utils"

	// "encoding/json"

	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
)

// 提供一些好玩的功能

// @Summary	AI聊天
// @Description
// @Tags		Tool
// @Accept		json
// @Produce	json
// @Param		ChatReq	body		request.ChatReq	true	"-"
// @Success	200			{object}	response.Response
// @Router		/api/tool/chat/ [post]
func AIChat(ctx *gin.Context) {
	logger := utils.GetLogInstance()
	req := new(request.ChatReq)
	if err := ctx.ShouldBindBodyWith(req, binding.JSON); err != nil {
		logger.Errorf("call ShouldBindWith failed, err = %s", err.Error())
		response.ResponseError(ctx, constanct.InvalidParamCode)
		return
	}
	chater,ok := logic.ChatMap[logic.ModelType(req.ModelType)]
	if !ok {
		logger.Errorf("call logic.ChatMap failed, req=%+v", utils.Sdump(req))
		response.ResponseError(ctx, constanct.InvalidParamCode)
		return
	}
	resp, err := chater.Chat(req.Message)
	if err != nil {
		logger.Errorf("call chater.Chat failed, req=%+v, err=%s", utils.Sdump(req), err.Error())
		response.ResponseError(ctx, constanct.ServerErrorCode)
		return
	}
	res:=utils.Resp4Map(ctx, resp,response.CreateResponse(constanct.SuccessCode))
	response.ResponseOK(ctx, res)
}

// @Summary	AI聊天开启/关闭
// @Description
// @Tags		Tool
// @Accept		json
// @Produce	json
// @Param		UpdateAIChatStateReq	body		request.UpdateAIChatStateReq	true	"-"
// @Success	200			{object}	response.Response
// @Router		/api/tool/chat/ [put]
func UpdateAIChatState(ctx *gin.Context) {
	logger := utils.GetLogInstance()
	admin:=middlewares.CheckUserHasPermission(ctx,constanct.SuperAdmin)
	if !admin {
		logger.Errorf("用户无权限访问, user=%s", middlewares.GetUid(ctx))
		response.ResponseError(ctx, constanct.AUTH_Token_URLVerifyCode)
		return
	}
	req := new(request.UpdateAIChatStateReq)
	if err := ctx.ShouldBindBodyWith(req, binding.JSON); err != nil {
		logger.Errorf("call ShouldBindWith failed, err = %s", err.Error())
		response.ResponseError(ctx, constanct.InvalidParamCode)
		return
	}
	chater := logic.ChatMap[logic.ModelType(req.ModelType)]
	if req.Available {
		chater.Open()
	} else {
		chater.Close()
	}
	response.ResponseOK(ctx, response.CreateResponse(constanct.SuccessCode))
}

// @Summary	AI聊天 模型列表
// @Description
// @Tags		Tool
// @Accept		json
// @Produce	json
// @Success	200			{object}	response.GetAIChatListResp
// @Router		/api/tool/chat [get]
func GetAIChatList(ctx *gin.Context) {
	resp:=response.GetAIChatListResp{}
	for k,v := range logic.ChatMap {
		resp.ChatList = append(resp.ChatList, response.ChatListItem{ModelType:string(k), Available:v.GetAvailable()})
	}
	resp.Response=response.CreateResponse(constanct.SuccessCode)
	response.ResponseOK(ctx, resp)
}

// @Summary	Sim代码查重
// @Description
// @Tags		Tool
// @Accept		json
// @Produce	json
// @Success	200			{object}	response.SimResp
// @Router		/api/tool/sim [post]
func Sim(ctx *gin.Context) {
	logger := utils.GetLogInstance()
	req := new(request.SimReq)
	if err := ctx.ShouldBindBodyWith(req, binding.JSON); err != nil {
		logger.Errorf("call ShouldBindWith failed, err = %s", err.Error())
		response.ResponseError(ctx, constanct.InvalidParamCode)
		return
	}
	resp,err:= logic.Sim(ctx,req)
	if err != nil {
		logger.Errorf("call logic.Sim failed, req=%+v, err=%s", utils.Sdump(req), err.Error())
		response.ResponseError(ctx, constanct.ServerErrorCode)
		return
	}
	response.ResponseOK(ctx, resp)
}